package com.lgkj.ui;

import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.lgkj.MainActivity;
import com.lgkj.R;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/26 0026.
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.cb_pwd)
    CheckBox cbPwd;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        updata();
    }

    @OnClick({R.id.iv_back, R.id.cb_pwd, R.id.tv_forget_pwd, R.id.ibtn_save, R.id.tv_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.cb_pwd:
                if (cbPwd.isChecked()) {
                    etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                etPassword.setSelection(etPassword.getText().toString().length());
                break;
            case R.id.tv_forget_pwd:
                startsActivity(new Intent(LoginActivity.this, ForgetPwdActivity.class));
                break;
            case R.id.ibtn_save:
                if (CommonUtils.isclick())
                    login();
                break;
            case R.id.tv_register:
                startsActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
        }


    }

    /**
     * 1男2女0保密
     */
    private void login() {
        showDiaLog("正在登录");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(LoginActivity.this));
        map.put("phone", etPhone.getText().toString());
        map.put("password", etPassword.getText().toString());
        HttpUtils.postHttp(map, CommonConstant.login, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                String isLogin = "1";
                dismissDiaLog();
                toastLong(jsonObject.optString("msg"), 0);
                if (jsonObject.optInt("code") == 0) {
                    String nickname = jsonObject.optJSONObject("data").optString("nickname");
                    String phone = jsonObject.optJSONObject("data").optString("phone");
                    String sex = jsonObject.optJSONObject("data").optString("sex");
                    String headimg = jsonObject.optJSONObject("data").optString("headimg");
                    String shop_id = jsonObject.optJSONObject("data").optString("shop_id");
                    String shop_table = jsonObject.optJSONObject("data").optString("shop_table");
                    int level = jsonObject.optJSONObject("data").optInt("level");
                    Log.i(TAG, "successHttp: " + shop_id+"=="+level);
                    SharedPreferencesUtils.setParam(LoginActivity.this, "phone", phone);
                    SharedPreferencesUtils.setParam(LoginActivity.this, "nickname", nickname);
                    SharedPreferencesUtils.setParam(LoginActivity.this, "headimg", headimg);
                    SharedPreferencesUtils.setParam(LoginActivity.this, "sex", sex);
                    SharedPreferencesUtils.setParam(LoginActivity.this, "password", etPassword.getText().toString());
                    SharedPreferencesUtils.setParam(LoginActivity.this, "shop_id", shop_id);
                    SharedPreferencesUtils.setParam(LoginActivity.this, "loadheadimg", "");
                    SharedPreferencesUtils.setParam(LoginActivity.this, "shop_table", shop_table);
                    SharedPreferencesUtils.setParam(LoginActivity.this, "level", level+"");
                    isLogin="0";
                    startsActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                }
                SharedPreferencesUtils.setParam(LoginActivity.this, "is_login", isLogin);
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }
}
